<?php
namespace app\zhongmaotong\controller;
use app\zhongmaotong\model\BannerModel;
use app\zhongmaotong\validate\BannerValidate;
use cmf\controller\AdminBaseController;

class BannerController extends AdminBaseController{
    public function index(){
        $lb=input('lb','');
        $where=[];
        if($lb){
            $where[]=['url','like',"%$lb%"];
        }
        $banner=BannerModel::order('list_order','desc')
            ->where($where)
            ->paginate(10);
        $this->assign('banner',$banner);

        return $this->fetch();
    }
    public function add(){
        return $this->fetch();
    }
    public function addPost(){
        //接收
        $data=input('post.');
        $v=new BannerValidate();
        $r=$v->scene('add')->check($data);
        if(!$r){
            $this->error($v->getError());
        }

        $model=BannerModel::create($data);
        if($model){
            $this->success('添加成功','Banner/index');
        }else{
            $this->error('添加失败');
        }
    }
    public function edit(){
        $id = input('id');
        $banner = BannerModel::where('id', '=', $id)
            ->find();
        $this->assign('banner', $banner);
        return $this->fetch();
    }
    public function editPost(){
        //接收
        $data=input('post.');
        $v=new BannerValidate();
        $r=$v->scene('edit')->check($data);
        if(!$r){
            $this->error($v->getError());
        }
        $model=BannerModel::find($data['id']);

        $model->url=$data['url'];
        $model->image=$data['image'];
        if($model->save()){
            $this->success('保存成功','Banner/index');
        }else{
            $this->error('保存失败');
        }
    }
    public function del(){
        $id=input('id/d');
        $model=BannerModel::destroy($id);
        if($model){
            $this->success('删除成功','Banner/index');
        }else{
            $this->error('删除失败');
        }
    }
    //排序
    public function px(){
        $model=new BannerModel();
        parent::listOrders($model);
        $this->success('排序成功');
    }

}